<?php

namespace App\Http\Controllers;

use App\Lib\DemoBase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
#use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Application;
use Spatie\RouteAttributes\Attributes\Any;
use Spatie\RouteAttributes\Attributes\Prefix;

/**
 * 使用手册
 */
#[Prefix('com')]
class CommonController extends BaseController
{
    public function index()
    {
        return date('c');
        $data = "task index";
        return response()->json($data);
    }

    /**
     * 获取路由定义的参数
     */
    public function read($id)
    {
        return "id: $id";
    }


    public function show()
    {
        return "task show";
    }

    //请求的常用方法
    public function param($id)
    {

        $data['route param'] = [$id];

        dump($data);
        //sreturn view('def');
    }

    /**
     * 使用request() 无法获获取路由定义的参数,即使控制器方法声明了参数
     * http://localhost:8000/task/params3/jazz/swing
     */
    public function params1($cate1, $cate2)
    {
        $data['request->query()'] = request()->query();
        $data['argv'] = func_get_args();
        dump($data);
    }

    /**
     * 使用request() 无法获取路由定义的参数
     * task/params2/{cate1}/{cate2}
     */
    public function params2()
    {
        $data['request->query()'] = request()->query();
        $data['route params'] = request()->route()->parameters();
        $data['route cate1'] = request()->route('cate1');
        $data['route parameter cate1'] = request()->route()->parameter('cate1');
        $data['argv'] = func_get_args();
        dump($data);
    }

    //url的获取与生成
    public function url($param1= null)
    {
        $url['id'] = $param1;
        //通过自定义的路由名称获取url地址
        //$url['task.url'] = route('task.url', ['id' => 10]);
        $url['route(task.url, [])'][] = route('task.index', [],false);
        $url['route(mini.index)'] = route('mini.index');
        //url()生成地址
        $url['url("task/param")'] = url('task/param');
        $url['url()->current()'] = url()->current(); //$request->url()
        $url['url()->full()'] = url()->full(); //$request->fullUrl()
        $url['Route::current()->uris'] = \Route::current()->uri;
        return $url;
    }

    //控制器内跳转
    function redirect()
    {
        $url = route('task.index');
        $url = route('task.index', [],false);
        //return redirect($url);
        //return redirect('task');
        //return redirect()->to('taskindex');
        //return redirect()->route('task.index'); //通过路由定义名字跳转
        //返回到上个页面
        //return redirect()->back();
        //return back();
        //通过action跳转
        //return redirect()->action([self::class, 'view']);
        //外部跳转
        return redirect()->away('https://www.baidu.com');
    }

    //获取路由名字
    function route()
    {
        //当前uri地址
        $route['uri'] = Route::current()->uri;
        $route['route.name'] = Route::currentRouteName(); //当前路由定义名字
        $route['route.action'] = Route::currentRouteAction(); //当前路由action
        dump($route);
    }

    //请求的常用方法
    public function myrequest(Request $request)
    {
        $this->request->path();   //控制器没有request属性
        $data['all'] = $request->all();
        $data[] = $request->input('name');
        $data['query()'] = $request->query();
        $data['请求路径:$request->path()'] = $request->path();
        $data['请求url 不带参数:$request->url()'] = $request->url();
        $data['请求url: $request->fullUrl()'] = $request->fullUrl();

        dump($data);
        //sreturn view('def');
    }

    //格式化响应
    function out()
    {
        $data = 'out';
        $data = response($data);
//        //输出json
        $data = [1,2,3];
//        $data = response($data);
//        $data = response()->json($data);
//        //指定响应码
//        $data = response($data, 201);
//        //指定header
        //$data = response('<b>hello</b>');
        //$data = response('<b>hello</b>')->header('content-type', 'text/plain');

        //指定视图
        //$data = view('task', ['id' => 'out']);
        $data = response()->view('task', ['id' => 'out']);
        return $data;
    }

    //常用功能
    function fun()
    {
        //负数转换
        $data[] = Str::plural('child');
        dump($data);
    }




    /**
     * larval 还不支持属性注解
     * The application instance being facaded.
     *
     * @var  \Illuminate\Container\Container
     */
    protected  $app;

    function app()
    {
        dd($this->app);

    }

    /**
     * laravel 5.5 开始支持依赖注入
     * @param Application $app
     * @return array
     */

    function container(Application $app)
    {
        $data['$app[config]'] = $app['config']->get('app.env');
        $data[' app(config)'] = app('config')->get('app.env');
        $data['获取依赖容器的实例的名字'] = \App::getBindings();
        return $data;
    }

    /**
     * $app->environmentFile() .env
     * $app->environmentPath()  Users/jiangyong/whole/data/wwwroot/laravel/demo
     * $app->environmentFilePath() Users/jiangyong/whole/data/wwwrsot/laravel/demo/.env
     */
    function envFile(Application $app)
    {
        putenv('env1=putenv'); //putenv() 只能getenv() 获取 $_ENV没有
        $data = [
            $app->environmentFile(), $app->environmentPath(),  $app->environmentFilePath(),
            env('APP_ENV'),getenv('APP_ENV'),
            env('APP_NAME'),
        getenv('env1'),env('env1'),$_SERVER['env1'] ?? '$_SERVER[evn1] not exist', $_ENV['env1'] ?? '$_ENV[env1] not exist',
        'env' => $_ENV,
        'server' => $_SERVER,
         ];
        return $data;
    }

    #[Any('demoBase')]
    function demoBase()
    {
        $data = ['id' =>1, 'mobile' => 'xxx', 'name' => 'lisi'];
        $obj = new DemoBase($data);
        foreach ($obj as $k => $v) {
            dump($k, $v);
        }
    }
}
